package net.skystudio.es.controller;

import com.alibaba.fastjson.JSON;
import net.skystudio.es.common.AbstractController;
import net.skystudio.es.entity.OperateLog;
import net.skystudio.es.entity.SystemLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

import static net.skystudio.es.constant.KafkaTopic.OPERATE_LOG_TOPIC;
import static net.skystudio.es.constant.KafkaTopic.SYSTEM_LOG_TOPIC;

@RestController
@RequestMapping("/kafka")
public class kafkaProductController extends AbstractController {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @PostMapping(value = "/systemLog/send")
    public ResponseResult<SystemLog> sendSystemLog(@RequestBody SystemLog systemLog) {
//        SystemLog systemLog = buildSystemLog();
        kafkaTemplate.send(SYSTEM_LOG_TOPIC, JSON.toJSONString(systemLog));
        return this.buildResponseResult(systemLog);
    }

    @PostMapping(value = "/operateLog/send")
    public ResponseResult<OperateLog> sendOperateLog(@RequestBody OperateLog operateLog) {
//        OperateLog operateLog = buildOperateLog();
        kafkaTemplate.send(OPERATE_LOG_TOPIC, JSON.toJSONString(operateLog));
        return this.buildResponseResult(operateLog);
    }

    private SystemLog buildSystemLog() {
        SystemLog systemLog = new SystemLog();
        systemLog.setId(UUID.randomUUID().toString());
        systemLog.setOperateTime(new Date());
        systemLog.setOrderNo("10000003");
        systemLog.setRequester("运营平台");
        systemLog.setResponder("订单系统");
        systemLog.setRequestMethod("/getOrderDetail");
        systemLog.setRequestMode("POST");
        systemLog.setDuration(234234L);
        systemLog.setRequestResult("SUCCESS");
        return systemLog;
    }

    private OperateLog buildOperateLog() {
        OperateLog operateLog = new OperateLog();
        operateLog.setId(UUID.randomUUID().toString());
        operateLog.setActionType("上架");
        operateLog.setOperateTime(new Date());
        operateLog.setRemoteAddress("192.168.10.122");
        operateLog.setUserId("10001");
        operateLog.setUserName("李明");
        operateLog.setSystemName("订单系统");
        return operateLog;
    }


}
